package com.info.protocol.netty.client;

import com.info.protocol.netty.core.codec.CustomDecoder;
import com.info.protocol.netty.core.codec.CustomEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;

public class CustomClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ch.pipeline()
                /**
                 * LengthFieldBasedFrameDecoder解码器自定义长度解决TCP粘包黏包问题,又称为: 自定义长度解码器
                 * LengthFieldBasedFrameDecoder本质上是ChannelHandler,需要加入ChannelPipeline中，且位于链的头部.
                 *
                 */
                .addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 9, 0))
                .addLast(new LoggingHandler())
                .addLast(new CustomEncoder())
                .addLast(new CustomDecoder())
                .addLast(new CustomClientHandler());
    }
}
